package com.qiye.livevideo.utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dyvmsapi.model.v20170525.SingleCallByTtsRequest;
import com.aliyuncs.dyvmsapi.model.v20170525.SingleCallByTtsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.qiye.livevideo.model.Constant;

/**
 * @program: livevideo
 * @Date: 2019/5/3 15:44
 * @Author: tongling
 * @Description:
 */
public class AliDyvmsMessage {

    /**
     * 云通信产品-语音API服务产品名称（产品名固定，无需修改）
     */
    private static final String PRODUCT = "Dyvmsapi";
    /**
     * 产品域名,开发者无需替换
     */
    private static final String DOMAIN = "dysmsapi.aliyuncs.com";

    /**
     * AK信息
     */
    private static String accessKeyId = "LTAIThD3KM2viOhv";
    private static String accessKeySecret = "yourAccessKeySecret";

    private static String templeteCode = "TTS_164375799";


    public static IAcsClient getClient() {
        //设置访问超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");
        //初始化acsClient 暂时不支持多region
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        try {
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", PRODUCT, DOMAIN);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return new DefaultAcsClient(profile);
    }


    /**
     * 发送语音验证码
     * @param phone
     * @param code
     * @return
     */
    public static SingleCallByTtsResponse sendSms(String phone, String code) {
        IAcsClient acsClient = getClient();
        // 组装请求对象-具体描述见控制台-文档部分内容
        SingleCallByTtsRequest request = new SingleCallByTtsRequest();
        //必填-被叫显号,可在语音控制台中找到所购买的显号
        request.setCalledShowNumber("02560000000");
        //必填-被叫号码
        request.setCalledNumber(phone);
        //必填-Tts模板ID
        request.setTtsCode(templeteCode);
        //可选-当模板中存在变量时需要设置此值
        request.setTtsParam("{code:"+ code +"}");
        //可选-音量 取值范围 0--200
        request.setVolume(100);
        //可选-播放次数
        request.setPlayTimes(3);
        //可选-外部扩展字段,此ID将在回执消息中带回给调用方
        request.setOutId("yourOutId");
        //hint 此处可能会抛出异常，注意catch
        SingleCallByTtsResponse singleCallByTtsResponse = null;
        try {
            singleCallByTtsResponse = acsClient.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        if (singleCallByTtsResponse.getCode() != null && singleCallByTtsResponse.getCode().equals(Constant.OK)) {
            //请求成功
            System.out.println("语音文本外呼---------------");
            System.out.println("RequestId=" + singleCallByTtsResponse.getRequestId());
            System.out.println("Code=" + singleCallByTtsResponse.getCode());
            System.out.println("Message=" + singleCallByTtsResponse.getMessage());
            System.out.println("CallId=" + singleCallByTtsResponse.getCallId());
        }
        return singleCallByTtsResponse;
    }

    /**
     * 测试
     * @param args
     */
    public static void main(String[] args) {
        sendSms("15225252106", "458741");
    }
}
